<?php
namespace jy_ailiao;
defined('BY_JYA') or exit('error');
/*
	分销插件会员类
	如果将分销商各项数据在每次数据变动时计算好并存入数据库。
	那么分别需要做
		在设置上下级关系时，修改新上级的 下线数量、分销商数量、订单数量、订单佣金数量等数据
		在变更上下级关系时，修改原、新上级的各项参数
		在发生订单时，修改上级的订单相关参数
		在用户成为分销商时，修改上级的分销商数量相关数据
*/
class member extends \plugin{
	public function member_show($mid,$default_url='',$default=false){		
		$member=pdo_get('jy_ailiao_member',array('mid'=>intval($mid),'uniacid'=>$_SESSION['uniacid']),array('id','mobile','nickname','realname','avatar','sex'));
		if(!$member){
			$member=$default?:array('nickname'=>'无');
		}else{
			$member['nickname']=$member['realname']?:$member['nickname'];
			$member['avatar']=tomedia($member['avatar']);
		}
		return $member;
	}
	/*
		判断会员
	*/
	public function get_member($mid=0){
		$mid=$mid?:$_SESSION['uid'];
		if(!$mid){
			$_SESSION['forward_url']='jy_ailiao/member.index/';
			$res=cfc('core')->checklogin('','',true);
			return $res;
		}
		$member=$this->get('member',array('mid'=>$mid,'uniacid'=>$_SESSION['uniacid']));
		if($member['id']){
			return $member;
		}else{
			$member['mid']=$mid;
			$this->register($member);
			$member=$this->get('member',array('mid'=>$mid,'uniacid'=>$_SESSION['uniacid']));
		}
		return $member;
	}
	public function checkmyinfo($mid=0){
		$mid=$mid?:$_SESSION['uid'];
		$member=pdo_get('jy_ailiao_member',array('mid'=>$mid,'uniacid'=>$_SESSION['uniacid']));
		if(!$member['id']){
			return true;
		}
		if(!$member['nickname'] || !$member['mobile'] || !$member['avatar'] || !$member['sex'] || !$member['birth']|| !$member['city']){
			exi('请先完善个人基本信息（昵称,手机,头像,性别,年龄,城市）！','error','jy_ailiao/member.myinfo/');
		}
		return true;
	}
	public function get_tomember($mid=0){
		$mid=$mid?:$_SESSION['uid'];
		if(!$mid){
			$_SESSION['forward_url']='jy_ailiao/member.index/';
			cfc('core')->checklogin();
			return;
		}
		$member=$this->get('tomember',array('memberid'=>$mid,'uniacid'=>$_SESSION['uniacid']));
		if($member['id']){
			return $member;
		}else{
			$member=array(
				'memberid'=>$mid,
				'uniacid'=>$_SESSION['uniacid'],
				'createtime'=>TIMESTAMP
			);
			$this->insert('tomember',$member);
			$member=$this->get('tomember',array('memberid'=>$mid,'uniacid'=>$_SESSION['uniacid']));
		}
		return $member;
	}
	
	/*
		会员注册
	*/
	public function register($member=array()){
		if(!$member['uniacid']){
			$member['uniacid']=$_SESSION['uniacid'];
		}
		if(!$member['mid']){
			$member['mid']=$_SESSION['uid'];
		}
		if(!$member['createtime']){
			$member['createtime']=TIMESTAMP;
		}
		$ismid=$this->get('member',array('mid'=>$member['mid'],'uniacid'=>$_SESSION['uniacid']),array('mid'));
		if($ismid){
			exi('该会员已注册，请勿重复绑定！','error');
		}
		$info=model('core/member')->detail($member['mid'],array('fromuser','sex','mobile','avatar','nickname'));
		$member['nickname']=$info['nickname'];
		$member['avatar']=$info['avatar'];
		$member['sex']=$info['sex'];
		$member['fromuser']=$info['fromuser']?:$_SESSION['fromuser'];
		if($member['fromuser']){
			$this->upsharejifen($member['fromuser']);
		}
		if($this->setting['new_bi']){
			model('core/member')->member_credit($member['mid'],'credit3',$this->setting['new_bi'],true,'jy_ailiao','新用户送金币');
		}
		$this->insert('member',$member);
	}
	/*
		审核区域代理
	*/
	public function check_agent($agent_id,$status,$remark=''){
		if(!$agent_id || !$status){
			return;
		}
		$agent_area=$this->get('member',array('id'=>$agent_id));
		if(!$agent_area){
			return;
		}
		$update=array(
			'status'=>$status,
			'check_time'=>TIMESTAMP,
		);
		if($update['status']==1){
		}else{
			$update['remark']=$remark;
		}
		$this->update('member',$update,array('id'=>$agent_id));
		return true;
	}
	/*
		禁用代理商
	*/
	public function stop_agent($agent_id){
		$this->update('member',array('status'=>2),array('mid'=>$agent_id));
	}
	/*
		恢复代理商
	*/
	public function start_agent($agent_id){
		$this->update('member',array('status'=>1),array('mid'=>$agent_id));
	}

	/*
		升级分销商等级
		先检测会员身份是否为正常分销商
		然后获取分销商当前等级的下一等级升级条件
		然后逐个条件对比，看是否满足升级条件
		如果全部满足，则升级到相应等级
		
		何时触发升级分销商等级检测呢？
		
	*/
	public function update_agent_level($mid){
		$member=$this->get('member',array('mid'=>$mid));
		if($member['stop']){
			return;
		}
		$level=pdo_getcolumn('jy_ailiao_level',array('id'=>$member['level_id']),'level');
		$levels=$this->getall('level',array('uniacid'=>$_SESSION['uniacid'],'level >'=>$level));
		if(!$levels){
			return;
		}
		$up=false;
		foreach($levels as $lv){
			$ordermoney=pdo_getsum('jy_ailiao_pingorderlog',array('ispay'=>1,'mid'=>$member['mid']),'money');
			if($ordermoney>=$lv['fee_per']){
				$next_level=$lv;
				$up=true;
				break;
			}
		}
		if($up){
			$update=array(
				'level_id'=>$next_level['id'],
			);
			if($next_level['pingnum']){
				$update['pingnum +=']=$next_level['pingnum'];
			}
			if($next_level['siminum']){
				$update['siminum +=']=$next_level['siminum'];
			}
			if($next_level['siminum2']){
				$update['siminum2 +=']=$next_level['siminum2'];
			}
			$update['cansimi']=$next_level['cansimi'];
			$this->update('member',$update,array('mid'=>$mid));
		}
		return true;
	}
	/*
		获取完善率
	*/
	public function getpercent($member=array()){
		$percent=0;
		if(!$member){
			return $percent;
		}
		if($member['nickname']){
			$percent +=2;
		}
		if($member['mobile']){
			$percent +=5;
		}
		if($member['realname']){
			$percent +=5;
		}
		if($member['wechat']){
			$percent +=5;
		}
		if($member['city']){
			$percent +=5;
		}
		if($member['birth']){
			$percent +=5;
		}
		if($member['sex']){
			$percent +=5;
		}
		if($member['xueli']){
			$percent +=5;
		}
		if($member['shouru']){
			$percent +=5;
		}
		if($member['youboy']){
			$percent +=5;
		}
		if($member['wanboy']){
			$percent +=5;
		}
		if($member['goufang']){
			$percent +=5;
		}
		if($member['gouche']){
			$percent +=5;
		}
		if($member['jiguan']){
			$percent +=5;
		}
		if($member['height']){
			$percent +=5;
		}
		if($member['weight']){
			$percent +=5;
		}
		if($member['tixing']){
			$percent +=5;
		}
		if($member['xiyan']){
			$percent +=5;
		}
		if($member['hejiu']){
			$percent +=5;
		}
		if($member['xingzuo']){
			$percent +=5;
		}
		if($member['jh_time']){
			$percent +=3;
		}
		return $percent;
	}
	public function getlogs($parameter,$limit=5,$lat_lng=array(),$page=1,$limitjuli=0){
		$limitjuli=intval($limitjuli);
		if(intval($lat_lng['lat'])){
			$lat_lng=array('lat'=>$lat_lng['lat'],'lng'=>$lat_lng['lng']);
			$this->session('lat_lng',$lat_lng);
		}
		$lat_lng=$this->session('lat_lng');
		$total='';
		$pindex = max(1, intval($page));
		$parameter['limit']=$parameter['limit']?:array($pindex,$limit);
		$parameter['orderby']=$parameter['orderby']?:'createtime desc';
		$type=$parameter['params']['type'];
		unset($parameter['params']['type']);
		if($type==4){
			$parameter['params']['mid']=$_SESSION['uid'];
			unset($parameter['params']['status']);
			unset($parameter['params']['noshow']);
			unset($parameter['params']['sex']);
		}elseif($type==2){
			//关注
			$mids=pdo_getall('jy_ailiao_likes',array('mid'=>$_SESSION['uid']),'tomid','tomid');
			if($mids){
				$mids=array_keys($mids);
			}else{
				$mids=-1;
			}
			$parameter['params']['mid']=$mids;
		}elseif($type==3){
			//推荐，动态不能连在一起
			
		}elseif($type==5){
			//热推栏目，按动态点赞数排名
			$parameter['orderby']='';
		}
		$list=pdo_getslice('jy_ailiao_newslog',$parameter['params'],$parameter['limit'],$total,$parameter['fields'],$parameter['key'],$parameter['orderby']);
		$current_year=date('Y');
		$zhiweis=$this->model('model')->zhiweis();
		foreach($list as $k=>&$v){
			$member=pdo_get('jy_ailiao_member',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$v['mid']),array('id','avatar','nickname','city','lat_lng','birth','sex','zhiwei','height','star','paixu','status'));
			$member['avatar']=tomedia($member['avatar']);
			$member['birth']=json_decode($member['birth'],true);
			$member['age'] = $member['birth']['year']?$current_year - $member['birth']['year'] - 1:'**';
			$member['height']=$this->heights[$member['height']];
			$member['zhiwei']=$zhiweis[$member['zhiwei']];
			$isadd=pdo_get('jy_ailiao_likes',array('mid'=>$_SESSION['uid'],'tomid'=>$v['mid']));
			if($isadd['id']){
				$member['islike']=1;
			}else{
				$member['islike']=0;
			}
			$v['member']=$member;
			$isadd=pdo_get('jy_ailiao_likenews',array('mid'=>$_SESSION['uid'],'pid'=>$v['id']));
			if($isadd['id']){
				$v['islike']=1;
			}else{
				$v['islike']=0;
			}
			$v['likes']=pdo_count('jy_ailiao_likenews',array('uniacid'=>$_SESSION['uniacid'],'pid'=>$v['id']));
			$v['lat_lng']=json_decode($v['lat_lng'],true);
			if(!$v['lat_lng']['lng'] || !$v['lat_lng']['lat']){
				$v['juli']=cfc('tools')->getdistance($lat_lng['lng'],$lat_lng['lat'],$v['lat_lng']['lng'],$v['lat_lng']['lat']);
				$v['juli']=intval($v['juli']/1000)?:2;
			}else{
				$v['juli']='未知';
			}
			if($limitjuli && $v['juli']>$limitjuli){
				unset($list[$k]);
			}
			if($type==3){
				if($this->lastmid==$v['mid']){
					unset($list[$k]);
				}
				$this->lastmid=$v['mid'];
			}
			$v['paixu']=$v['juli']-$v['id'];
		}
		if($type==1){
			paixu($list,'juli');
		}elseif($type==5){
			paixu($list,'likes','desc');
		}else{
			paixu($list,'paixu');
		}
		if($limit){
			$list=array_slice($list,0,intval($limit));
		}else{
			$page=$page?:1;
			$start=($page-1)*10;
			$list=array_slice($list,$start,10);
		}
		return $list;
	}
	public function getmemberlist($parameter,$limit=5,$lat_lng=array(),$page=1,$limitjuli=0){
		$limitjuli=intval($limitjuli);
		if(intval($lat_lng['lat'])){
			$lat_lng=array('lat'=>$lat_lng['lat'],'lng'=>$lat_lng['lng']);
			$this->session('lat_lng',$lat_lng);
		}
		$lat_lng=$this->session('lat_lng');
		$total='';
		$parameter['orderby']=$parameter['orderby']?:'id desc';
		$order=$parameter['params']['order'];
		unset($parameter['params']['order']);
		if($order=='islike'){
			$mids=pdo_getall('jy_ailiao_likes',array('mid'=>$_SESSION['uid']),'tomid','tomid');
			if($mids){
				$mids=array_keys($mids);
			}else{
				$mids=-1;
			}
			$parameter['params']['mid']=$mids;
		}
		if($order=='istuijie'){
			$parameter['params']['istuijie']=1;
		}
		$list=pdo_getslice('jy_ailiao_member',$parameter['params'],$parameter['limit'],$total,$parameter['fields'],$parameter['key'],$parameter['orderby']);
		$current_year=date('Y');
		$zhiweis=$this->model('model')->zhiweis();
		foreach($list as $k=>&$member){
			$member['birth']=json_decode($member['birth'],true);
			$member['images']=json_decode($member['images'],true);
			$member['age'] = $member['birth']['year']?$current_year - $member['birth']['year'] - 1:'**';
			$member['height']=$this->heights[$member['height']];
			$member['zhiwei']=$zhiweis[$member['zhiwei']];
			$member['likes']=pdo_count('jy_ailiao_likes',array('tomid'=>$member['mid']));
			$member['lat_lng']=json_decode($member['lat_lng'],true);
			if($member['lat_lng']['lng'] && $member['lat_lng']['lat']){
				$member['juli']=cfc('tools')->getdistance($lat_lng['lng'],$lat_lng['lat'],$member['lat_lng']['lng'],$member['lat_lng']['lat']);
				$member['juli']=$member['juli']?price($member['juli']/1000,'int'):0;
			}else{
				$member['juli']=0;
			}
			if($limitjuli && $member['juli']>$limitjuli){
				unset($list[$k]);
			}
			$isadd=pdo_get('jy_ailiao_likes',array('mid'=>$_SESSION['uid'],'tomid'=>$member['mid']));
			if($isadd['id']){
				$member['islike']=1;
			}else{
				$member['islike']=0;
			}
			$member['paixu']=$member['juli'];
		}
		paixu($list,'paixu');
		if($limit){
			$list=array_slice($list,0,intval($limit));
		}else{
			$page=$page?:1;
			$start=($page-1)*10;
			$list=array_slice($list,$start,10);
		}
		return $list;
	}
	public function upfuwuid($mid=0,$fuwuid=0){
		$mid=$mid?:$_SESSION['uid'];
		$member=pdo_get('jy_ailiao_member',array('mid'=>$mid));
		if(!$member['fuwu_id'] && $fuwuid){
			pdo_update('jy_ailiao_member',array('fuwu_id'=>$fuwuid),array('mid'=>$mid));
		}
		return true;
	}
	public function upsharejifen($fromuser=0){
		if(!$fromuser){
			return false;
		}
		$pingset=$this->plugin_setting('ping');
		if(!$pingset['is_sharejifen']){
			return false;
		}
		$downnum=pdo_count('jy_ailiao_member',array('uniacid'=>$_SESSION['uniacid'],'fromuser'=>$fromuser),'id');
		foreach($pingset['sharejifenset'] as $item){
			if($downnum>=$item['start'] && $downnum<=$item['end']){
				$credit=$item['credit'];
				if($credit>0){
					$key='key'.$item['end'];
					$isadd=pdo_count('core_credit_log',array('tag'=>$key,'is_add'=>1,'plugin'=>'jy_ailiao','uniacid'=>$_SESSION['uniacid'],'mid'=>$fromuser),'id');
					if(!$isadd){
						model('core/member')->member_credit($fromuser,'credit1',$credit,true,'jy_ailiao','分享《'.$item['end'].'人阶梯》赚积分',$key);
					}
				}
			}
		}
		return true;
	}
	public function looklog($tomid=0){
		if(!$_SESSION['uid']){
			return '';
		}
		$looklog=pdo_get('jy_ailiao_looklog',array('tomid'=>$tomid,'mid'=>$_SESSION['uid'],'uniacid'=>$_SESSION['uniacid']),array('createtime'),'createtime desc');
		if($looklog['createtime']>TIMESTAMP-600){
			return '';
		}
		$insert=array(
			'uniacid'=>$_SESSION['uniacid'],
			'mid'=>$_SESSION['uid'],
			'tomid'=>$tomid,
			'createtime'=>TIMESTAMP,
		);
		pdo_insert('jy_ailiao_looklog',$insert);
		$basic=$this->plugin_setting('basic');
		if($basic['liao_look']){
			$tcp=$this->model('kefu')->gettcp(0,$tomid,'',1);
			$postdata=array(
				'tcpid'=>$tcp['id'],
				'tomid'=>$tomid,
				'msg'=>$basic['liao_lookmsg']?:'你好！欢迎浏览我的主页！',
				'type'=>0,
			);
			$res=$this->model('kefu')->addchatlog($postdata,1);
		}
		return $res['log']['msg'];
	}
	public function get_tanfans(){
		$params=array('status'=>0,'sex'=>$_SESSION['sex']==1?2:1,'uniacid'=>$_SESSION['uniacid']);
		if($this->setting['open_tanimg']==2){
			$mids=pdo_getall('jy_ailiao_likes',array('mid'=>$_SESSION['uid'],'uniacid'=>$_SESSION['uniacid']),'tomid','tomid');
			if($mids){
				$mids=array_keys($mids);
			}else{
				$mids=-1;
			}
			//$params['mid <>']=$mids;
		}
		if($this->session('pcd')){
			$params['city']=$this->session('pcd')['city'];
		}
		$orderby='id desc';
		$pindex = max(1, 1);
		$list=pdo_getslice('jy_ailiao_member',$params,array($pindex,6),$total,array('id','mid','nickname','avatar','city'),'',$orderby);
		if(!count($list)){
			unset($params['city']);
			$list=pdo_getslice('jy_ailiao_member',$params,array($pindex,6),$total,array('id','mid','nickname','avatar','city'),'',$orderby);
		}
		return $list;
	}
}